package com.example.wuyeguanli.activity;

import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.example.wuyeguanli.R;
import com.example.wuyeguanli.tools.ApiClient; // 引入ApiClient

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public class RegisterLoginActivityTest {

    private static MockWebServer mockWebServer;
    private static String originalBaseUrl; // 用于在测试后恢复原始URL

    @BeforeClass
    public static void setUpClass() throws IOException {
        // 1. 保存原始URL
        originalBaseUrl = ApiClient.BASE_URL;

        // 2. 启动MockWebServer
        mockWebServer = new MockWebServer();
        mockWebServer.start(8080); // 依然在8080端口启动

        // 3. 核心修正: 动态设置ApiClient的URL指向我们的MockWebServer
        //    MockWebServer.url("/") 会返回类似 "http://localhost:8080/" 的地址
        ApiClient.setBaseUrlForTesting(mockWebServer.url("/").toString());
    }

    @AfterClass
    public static void tearDownClass() throws IOException {
        if (mockWebServer != null) {
            mockWebServer.shutdown();
        }
        // 恢复原始的BASE_URL，避免影响其他测试类
        ApiClient.setBaseUrlForTesting(originalBaseUrl);
    }

    @Before
    public void setUp() {
        Intents.init();
    }

    @After
    public void tearDown() {
        Intents.release();
    }

    @Test
    public void testLogin_Success() {
        mockWebServer.enqueue(new MockResponse().setBody("{\"state\":\"success\",\"error\":\"\",\"data\":null}"));
        mockWebServer.enqueue(new MockResponse().setBody("{\"state\":\"success\",\"error\":\"\",\"data\":{\"phone\":\"13800138001\",\"username\":\"测试大师\"}}"));

        ActivityScenario.launch(RegisterLoginActivity.class);

        onView(withId(R.id.phone)).perform(typeText("13800138001"));
        onView(withId(R.id.password)).perform(typeText("password123"), closeSoftKeyboard());
        onView(withId(R.id.login)).perform(click());

        intended(hasComponent(MainActivity.class.getName()));
    }

    @Test
    public void testLogin_Fail_WrongPassword() {
        mockWebServer.enqueue(new MockResponse().setBody("{\"state\":\"error\",\"error\":\"账号或密码错误！\",\"data\":null}"));

        ActivityScenario.launch(RegisterLoginActivity.class);

        onView(withId(R.id.phone)).perform(typeText("13800138001"));
        onView(withId(R.id.password)).perform(typeText("wrongpassword"), closeSoftKeyboard());
        onView(withId(R.id.login)).perform(click());

        onView(withId(R.id.login)).check(androidx.test.espresso.assertion.ViewAssertions.matches(isDisplayed()));
    }
}